package cn.hh.org.service.impl;

import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.basic.util.PageList;
import cn.hh.org.domain.Department;
import cn.hh.org.domain.Employee;
import cn.hh.org.mapper.DeptMapper;
import cn.hh.org.mapper.EmpMapper;
import cn.hh.org.query.DeptQuery;
import cn.hh.org.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl extends BasicServiceImpl<Department> implements IDeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Department> deptTree() {
        return deptMapper.deptTree();
    }

    @Override
    public void add(Department department) {
        deptMapper.add(department);

        if (department.getParent()==null ||department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirpath
            Department dept = deptMapper.loadById(department.getParent().getId());
            department.setDirPath(dept.getDirPath()+"/"+department.getId());
        }
        //更新数据
        deptMapper.update(department);
    }
    @Override
    public void update(Department department) {
        //没有选择上级部门的情况
        if (department.getParent()==null ||department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirpath
            Department dept = deptMapper.loadById(department.getParent().getId());
            department.setDirPath(dept.getDirPath()+"/"+department.getId());
        }
        deptMapper.update(department);
    }

    @Override
    public PageList<Employee> findEmpById(DeptQuery query) {
        Integer total=empMapper.queryCount(query);
        List<Employee> bags = empMapper.queryData(query);
        PageList<Employee> pageList = new PageList<>(total, bags);
        return pageList;
    }
}
